<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const directLine = WebChat.createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() });
        const store = testHelpers.createStore();

        const telemetryMeasurements = [];

        WebChat.renderWebChat(
          {
            directLine,
            store,
            onTelemetry: event => {
              const { data, dimensions, duration, error, fatal, name, type, value } = event;

              telemetryMeasurements.push({
                data,
                dimensions,
                duration,
                error,
                fatal,
                name,
                type,
                value
              });
            },
            styleOptions: {
              sendAttachmentOn: 'attach'
            }
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        await pageObjects.uploadFile('seaofthieves.jpg');

        await pageConditions.minNumActivitiesShown(2);
        await pageConditions.allOutgoingActivitiesSent();
        await pageConditions.allImagesLoaded();

        const sendFileTelemetryMeasurements = telemetryMeasurements.filter(({ name }) =>
          ['init', 'sendFiles:makeThumbnail', 'submitSendBox'].includes(name)
        );

        expect(sendFileTelemetryMeasurements).toHaveProperty('length', 4);
        expect(sendFileTelemetryMeasurements[2]).toHaveProperty('name', 'sendFiles:makeThumbnail');
        expect(sendFileTelemetryMeasurements[2]).toHaveProperty('type', 'timingend');

        sendFileTelemetryMeasurements[2].duration = 1000;

        expect(sendFileTelemetryMeasurements).toEqual([
          {
            data: undefined,
            dimensions: {
              'capability:downscaleImage:workerType': 'web worker',
              'capability:renderer': 'html',
              'prop:locale': 'en-US',
              'prop:speechRecognition': 'false',
              'prop:speechSynthesis': 'false'
            },
            duration: undefined,
            error: undefined,
            fatal: undefined,
            name: 'init',
            type: 'event',
            value: undefined
          },
          {
            data: undefined,
            dimensions: {
              'capability:downscaleImage:workerType': 'web worker',
              'capability:renderer': 'html',
              'prop:locale': 'en-US',
              'prop:speechRecognition': 'false',
              'prop:speechSynthesis': 'false'
            },
            duration: undefined,
            error: undefined,
            fatal: undefined,
            name: 'sendFiles:makeThumbnail',
            type: 'timingstart',
            value: undefined
          },
          {
            data: undefined,
            dimensions: {
              'capability:downscaleImage:workerType': 'web worker',
              'capability:renderer': 'html',
              'prop:locale': 'en-US',
              'prop:speechRecognition': 'false',
              'prop:speechSynthesis': 'false'
            },
            duration: 1000,
            error: undefined,
            fatal: undefined,
            name: 'sendFiles:makeThumbnail',
            type: 'timingend',
            value: undefined
          },
          {
            data: {
              numFiles: 1,
              sumSizeInKB: 379
            },
            dimensions: {
              'capability:downscaleImage:workerType': 'web worker',
              'capability:renderer': 'html',
              'prop:locale': 'en-US',
              'prop:speechRecognition': 'false',
              'prop:speechSynthesis': 'false'
            },
            duration: undefined,
            error: undefined,
            fatal: undefined,
            name: 'submitSendBox',
            type: 'event',
            value: undefined
          }
        ]);

        await host.snapshot();
      });
    </script>
  </body>
</html>
